package com.weidft.dal.warehouse.dao.ibatis;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.weidft.dal.warehouse.dao.IPokouDao;
import com.weidft.dal.warehouse.daoobject.PokouDO;
import com.weidft.dal.warehouse.daoobject.ProductRuDO;
import com.weidft.web.biz.WDViewTools2;

public class IBatisPokouDao extends SqlMapClientDaoSupport implements  IPokouDao{
	
	private ProductRuDO convertOldProductRuDO(PokouDO pokouDO)
	{
		ProductRuDO p1 = new ProductRuDO(); 
    	p1.setJobNo(pokouDO.getOldJobNo());
    	p1.setGmtRu(pokouDO.getGmtChange());
    	p1.setMaterial(pokouDO.getOldMaterial());
    	p1.setDia(pokouDO.getOldDia());
    	p1.setThickness(pokouDO.getOldThickness());
    	p1.setWeight(pokouDO.getOldWeight());
    	p1.setRealAmount(-pokouDO.getAmount()*1f);
    	p1.setType(WDViewTools2.RUKU_NORMAL);
    //	p1.setOrigin(WDViewTools2.TYPE_POKOU);
    	p1.setNote(pokouDO.getNote());
    	p1.setAllWeight(pokouDO.getAllWeight());
    	return p1;
	}
    
	private ProductRuDO convertNewProductRuDO(PokouDO pokouDO)
	{
		ProductRuDO p1 = new ProductRuDO(); 
    	p1.setJobNo(pokouDO.getNewJobNo());
    	p1.setGmtRu(pokouDO.getGmtChange());
    	p1.setMaterial(pokouDO.getNewMaterial());
    	p1.setDia(pokouDO.getNewDia());
    	p1.setThickness(pokouDO.getNewThickness());
    	p1.setWeight(pokouDO.getNewWeight());
    	p1.setRealAmount(pokouDO.getAmount()*1f);
    	p1.setType(WDViewTools2.RUKU_NORMAL);
    	//p1.setOrigin(WDViewTools2.TYPE_POKOU);
    	p1.setNote(pokouDO.getNote());
    	p1.setAllWeight(pokouDO.getAllWeight());
    	return p1;
	}
    
	public Integer insertPokouDO(PokouDO pokouDO)
    {
    	int id =  (Integer) getSqlMapClientTemplate().insert("POKOU.insert",pokouDO);    	
    	ProductRuDO p1 = convertOldProductRuDO(pokouDO);
    	ProductRuDO p2 = convertNewProductRuDO(pokouDO);
    	p1.setOriginId(id);
    	p2.setOriginId(id);
    	getSqlMapClientTemplate().insert("PRODUCT_RU.insert",p1);
    	getSqlMapClientTemplate().insert("PRODUCT_RU.insert",p2);
    	return id;
    }

    public Integer countPokouDO(PokouDO pokouDO)
    {
    	return (Integer) getSqlMapClientTemplate().queryForObject("POKOU.count", pokouDO);
    }

    @SuppressWarnings("unchecked")
	public List<PokouDO> listPokouDO(PokouDO pokouDO)
    {
        return getSqlMapClientTemplate().queryForList("POKOU.list",pokouDO);
    }

    public PokouDO getPokouDOById(int id)
    {
    	return (PokouDO) getSqlMapClientTemplate().queryForObject("POKOU.findById", id);
    }

    public void updatePokouDO(PokouDO pokouDO)
    {
    	getSqlMapClientTemplate().update("POKOU.update",pokouDO);
    	ProductRuDO p1 = convertOldProductRuDO(pokouDO);
    	ProductRuDO p2 = convertNewProductRuDO(pokouDO);
    	p1.setOriginId(pokouDO.getId());
    	p2.setOriginId(pokouDO.getId());
    	getSqlMapClientTemplate().update("PRODUCT_RU.updateByOriginID",p1);
    	getSqlMapClientTemplate().update("PRODUCT_RU.updateByOriginID",p2);
    }

    public void deletePokouDO(int id)
    {
    	getSqlMapClientTemplate().delete("POKOU.deleteById",id);
    	getSqlMapClientTemplate().delete("PRODUCT_RU.deleteByOriginID",id);
    	getSqlMapClientTemplate().delete("PRODUCT_RU.deleteByOriginID",id);
    }
}
